package logic

import (
	"context"
	"errors"
	"video-hub/models/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoDetailLogic {
	return &VideoDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频详情
func (l *VideoDetailLogic) VideoDetail(in *videorpc.VideoDetailRequest) (*videorpc.VideoDetailResponse, error) {
	// todo: add your logic here and delete this line

	var video mysql.Video
	err := video.FirstVideoById(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("该视频不存在")
	}
	err = mysql.UpdatePlayNumAdd(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("浏览量+1失败")
	}
	var list *videorpc.VideoDetailResponse
	list = video.FindVideoDetail(mysql.DB, in.VideoId)

	return &videorpc.VideoDetailResponse{
		Username:      list.Username,
		VideoId:       list.VideoId,
		VideoTitle:    list.VideoTitle,
		VideoCategory: list.VideoCategory,
		VideoLink:     list.VideoLink,
		CoverLink:     list.CoverLink,
		VideoIntro:    list.VideoIntro,
		TopicName:     list.TopicName,
		PlayNum:       list.PlayNum,
		LikeNum:       list.LikeNum,
		CollectNum:    list.CollectNum,
		CommentNum:    list.CommentNum,
	}, nil
}
